package com.foreknow.service.impl;

import com.foreknow.dao.BaseDao;
import com.foreknow.dao.impl.userDaoImpl;
import com.foreknow.model.User;
import com.foreknow.service.userService;

import java.sql.SQLException;

public class UserServiceImpl implements userService {
    private userDaoImpl dao;

    public UserServiceImpl() {
        dao = new userDaoImpl();
    }

    @Override
    public User Login(String username, String password) {

        User user = null;
        try {
            user = dao.queryOne("select*from user where username=? and password=?", username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public boolean Registered(User[] users) {
        String sql = "insert into user(username,account,password,phone,email,avater) values(?,?,?,?,?,?)";
        BaseDao<User> userBaseDao = new userDaoImpl();
        int isRight = 0;
        try {
            isRight = userBaseDao.insert(sql, users);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isRight == 1;
    }

    public User getUserById(int id) {
        String sql = "select * from user where id=?";
        try {
            return dao.queryOne(sql, id);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public User getUserByName(String username) {
        String sql = "select * from user where username like ? or email like ? or account like ? or phone like";
        Object[] params = {username, username, username, username};
        try {
            return dao.queryOne(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }
    public Integer updateUser(User user){
        String sql="update user set username = ?,account = ?,password = ?,phone = ?,email = ? where id = ?";
        try {
            return dao.update(sql,user.getUsername(),user.getAccount(),user.getPassword(),user.getPhone(),user.getEmail(),user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

}




